package com.nuoniu.sibanyun.controller.erp.in;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.UsedGradeRuleBo;
import com.nuoniu.sibanyun.entity.erp.in.InRdStyle;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpAccountingService;
import com.nuoniu.sibanyun.service.erp.IErpGradeRuleService;
import com.nuoniu.sibanyun.service.erp.in.IInRdStyleService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 出入库类型 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-09-25
 */
@RestController
@RequestMapping("/in/rdStyle")
@Api(value = "出入库类型管理", tags = "出入库类型管理")
public class InRdStyleController extends BaseController {

    @Autowired
    private IInRdStyleService rdStyleService;
    @Autowired
    private IErpAccountingService accountingService;
    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IErpGradeRuleService erpGradeRuleService;

    /**
     * 获取出入库类别
     * @author 小啊刚
     * @date 2020-09-25
     */
    @ApiOperation("获取出入库类别")
    @GetMapping("pageStyleList")
    public HttpResult<InRdStyle> getStyleList(Page page, InRdStyle rdStyle) {
        rdStyle.setAccountId(accountService.getCurrentAccountId());
        return HttpResult.ok(rdStyleService.getStyleList(page, rdStyle));
    }

    /**
     * 保存出入库类别
     * @author 小啊刚
     * @date 2020-09-26
     */
    @PostMapping("save")
    @ApiOperation("保存出入库类别")
    public HttpResult save(@RequestBody InRdStyle inRdStyle) {
        if(StringUtils.isBlank(inRdStyle.getName())) {
            throw new NuoNiuException("出入库类别名称不能为空");
        }
        if(StringUtils.isBlank((inRdStyle.getCode()))) {
            throw new NuoNiuException("出入库类别编码不能为空");
        }

        //检查编码规则
        InRdStyle parentRdStyle = rdStyleService.getById(inRdStyle.getParentId());
        UsedGradeRuleBo usedGradeRuleBo = new UsedGradeRuleBo();
        if(parentRdStyle != null) {
            usedGradeRuleBo = erpGradeRuleService.checkCode(inRdStyle.getCode(), parentRdStyle.getCode(), parentRdStyle.getDepth(), "rd_style");
        }else {
            usedGradeRuleBo = erpGradeRuleService.checkCode(inRdStyle.getCode(), null, null, "rd_style");
        }
        inRdStyle.setGradeRuleLevel(usedGradeRuleBo.getGradeRuleLevel());
        inRdStyle.setGradeRuleStr(usedGradeRuleBo.getGradeRuleStr());

        UserInfo userInfo = UserThreadLocal.get();
        inRdStyle.setCompanyId(userInfo.getCompanyId());
        InRdStyle styleByCode = rdStyleService.getByCode(inRdStyle);
        InRdStyle styleByName = rdStyleService.getByName(inRdStyle);
        if(inRdStyle.getId() != null) {
            if(styleByCode != null && !(styleByCode.getId().equals(inRdStyle.getId()))) {
                throw new NuoNiuException("编码已存在");
            }
            if(styleByName != null && !(styleByName.getId().equals(inRdStyle.getId()))) {
                throw new NuoNiuException("名称已存在");
            }

            //修改下级的父级ids
            if(parentRdStyle != null) {
                if((inRdStyle.getId()).equals(parentRdStyle.getParentId())) {
                    throw new NuoNiuException("父级不能选择自己");
                }
                rdStyleService.updateChildrenParentIds(inRdStyle, parentRdStyle);
                inRdStyle.setUpdateDate(LocalDateTime.now());
                inRdStyle.setDepth(parentRdStyle.getDepth() + 1);
            }else {
                inRdStyle.setDepth(1);
            }
            inRdStyle.setUpdateUserId(userInfo.getUserId());
            rdStyleService.updateById(inRdStyle);
        }else {
            if(styleByCode != null) {
                throw new NuoNiuException("编码已存在");
            }
            if(styleByName != null) {
                throw new NuoNiuException("名称已存在");
            }

            if(parentRdStyle != null) {
                if (parentRdStyle.getParentIds().endsWith(",")) {
                    inRdStyle.setParentIds(parentRdStyle.getParentIds() + parentRdStyle.getId() + ",");
                }else {
                    inRdStyle.setParentIds(parentRdStyle.getParentIds() + "," + parentRdStyle.getId() + ",");
                }
                inRdStyle.setTreeNames(parentRdStyle.getTreeNames() + "/" + inRdStyle.getName());
                //修改父级
                parentRdStyle.setIsEndNode(false);
                rdStyleService.updateById(parentRdStyle);
                inRdStyle.setDepth(parentRdStyle.getDepth() + 1);
            }else {
                inRdStyle.setTreeNames(inRdStyle.getName());
            }
            inRdStyle.setCreateUserId(userInfo.getUserId());
            inRdStyle.setCompanyId(userInfo.getCompanyId());
            inRdStyle.setAccountId(accountService.getCurrentAccountId());
            rdStyleService.save(inRdStyle);
        }
        return HttpResult.ok();
    }

    /**
     * 删除出入库类别
     * @author 小啊刚
     * @date 2020-09-23
     */
    @PostMapping("delete")
    @ApiOperation(value = "删除出入库类别")
    public HttpResult delete(@RequestBody List<Long> ids) {
        if(ids.size() == 0) {
            throw new NuoNiuException("请选择要删除的出入库类别");
        }

        rdStyleService.delete(ids.get(0));
        return HttpResult.ok();
    }
}
